package org.lxh;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
@SuppressWarnings("unchecked")
public class CreatQRCode {  
    //修改数据库连接 DB
	public static void main(String[] args) throws Exception {  
//      String text = "http://60.2.39.228:18080/x5/UI2/v_/elevator/mainActivity.w?device=m&id="; 
//        String text = "192.168.1.66:23008/x5/UI2/v_/ReportInfo/Index.w?device=m";
//		String text = "http://192.168.1.86:23008/x5/UI2/v_/guye/mainActivity.w?device=m&id=";  //芦台 服务器   
		String text = "http://yutian.tslanpu.cn:18080/x5/UI2/v_/yutian/mainActivity.w?device=m&id=";  //汉沽 服务器   
//        String text = "http://192.168.1.66:23008/x5/UI2/v_/hangu/mainActivity.w?device=m&id=";
        int width = 300;  
        int height = 300;  
        //二维码的图片格式  
        String format = "png";  
        Hashtable hints = new Hashtable();  
        //内容所使用编码  
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");  
  
    	Map<String,String> ids = getIds();
    	int j = 0;
    	for(String id : ids.keySet()){
    			BitMatrix bitMatrix = new MultiFormatWriter().encode(text+id,  
                    BarcodeFormat.QR_CODE, width, height, hints);  
    			
    		id = id.replace("/", "$");
    		
    		String address = ids.get(id);
    		try {
    			for(int i = 0; i < address.length(); i ++){
        			if(Character.isDigit(address.charAt(i))){
        				if(i != 0){
        					address = address.substring(0, i);
        					address = address.trim();
        				}
        				break;
        			}
        		}
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println(id.toString()+" "+address);
			}

    		
    		File fileDir = new File("e:"+File.separator+"QR4"+File.separator + address);
    		if( !fileDir.isDirectory()){
    			fileDir.mkdir();
    		}
    		 
    		 //二维码生成路径
    		 //File outputFile = new File("d:"+File.separator+"QR2"+File.separator + "梯冀"+ id + "&地址&" + ids.get(id)+".png");
    		 File outputFile = new File("e:"+File.separator+"QR4"+File.separator + address + File.separator + "梯冀"+ id + ".png");
    	     MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
    	     j++;
    	}
    	
//    	for(String id : ids){
//    		BitMatrix bitMatrix = new MultiFormatWriter().encode(text+id,  
//                    BarcodeFormat.QR_CODE, width, height, hints);  
//    		
//    		 File outputFile = new File("d:"+File.separator+"QR"+File.separator+id+".png");  
//    	     MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
//    		
//    	}
    	System.out.println(j);
    }  
	
    public static Map<String,String> getIds() throws SQLException{
    	Map<String,String> ids = new HashMap<String,String>();
    	Connection conn = DB.getConnection();
	    	String sql = "select * from elevatorinfor";
//   	String sql = "SELECT * FROM elevatorinfor WHERE address like '%婚庆广场仁和厅1#%'";
//   	String delsql = "DELETE FROM elevatorinfor WHERE address LIKE '%" +add+ "%'";
//    	String sql = "SELECT * FROM elevatorinfor WHERE department = '唐山永达房地产开发有限公司'";
//    	String sql = "SELECT * FROM elevatorinfor WHERE id = '梯冀GYE0061'";
    	PreparedStatement PST = DB.createPST(conn, sql);
    	ResultSet rs = DB.getRs(PST);
    	try {
			while(rs.next()){
				if(!rs.getString("id").equals("")){
					//id 格式为    梯冀CFD0029 截取为CFD0029
					//ids.put(rs.getString("id").substring(2), rs.getString("department")+rs.getString("address"));
					ids.put(rs.getString("id").substring(2), rs.getString("address"));
					
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return ids;
    }
  
}  